package cn.sciento.wechat.enterprise.conf;

import java.nio.charset.StandardCharsets;
import cn.sciento.starter.integrate.service.AbstractCorpSyncService;
import cn.sciento.wechat.enterprise.service.WechatCorpAddressService;
import cn.sciento.wechat.enterprise.service.WechatCorpAgentManageService;
import cn.sciento.wechat.enterprise.service.WechatCorpMessageService;
import cn.sciento.wechat.enterprise.service.WechatMaterialManageService;
import cn.sciento.wechat.enterprise.service.WechatTicketService;
import cn.sciento.wechat.enterprise.service.WechatTokenService;
import cn.sciento.wechat.enterprise.service.impl.WechatCorpAddressServiceImpl;
import cn.sciento.wechat.enterprise.service.impl.WechatCorpAgentManageServiceImpl;
import cn.sciento.wechat.enterprise.service.impl.WechatCorpMessageServiceImpl;
import cn.sciento.wechat.enterprise.service.impl.WechatCorpSyncServiceImpl;
import cn.sciento.wechat.enterprise.service.impl.WechatMaterialManageServiceImp;
import cn.sciento.wechat.enterprise.service.impl.WechatTicketServiceImp;
import cn.sciento.wechat.enterprise.service.impl.WechatTokenServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class WechatEnterpriseConfig {
  @Bean
  @ConditionalOnMissingBean(name = {"wdRedisTemplate"})
  public RedisTemplate wdRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
  }
  
  @Bean
  @ConditionalOnMissingBean(name = {"wdRestTemplate"})
  public RestTemplate wdRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    return restTemplate;
  }
  
  @Bean
  public AbstractCorpSyncService wechatCorpSyncService() {
    return (AbstractCorpSyncService)new WechatCorpSyncServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatCorpMessageService wechatCorpMessageService() {
    return (WechatCorpMessageService)new WechatCorpMessageServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatCorpAgentManageService wechatCorpAgentManageService() {
    return (WechatCorpAgentManageService)new WechatCorpAgentManageServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatCorpAddressService wechatCorpAddressService() {
    return (WechatCorpAddressService)new WechatCorpAddressServiceImpl();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatTicketService wechatTicketService() {
    return (WechatTicketService)new WechatTicketServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatMaterialManageService wechatMaterialManageService() {
    return (WechatMaterialManageService)new WechatMaterialManageServiceImp();
  }
  
  @Bean
  @ConditionalOnMissingBean
  public WechatTokenService wechatTokenService() {
    return (WechatTokenService)new WechatTokenServiceImpl();
  }
}
